﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Dala.Samples.sitemap
{
    public partial class sitemap : System.Web.UI.Page
    {
        public class ProductCategory
        {
            public int CategoryId { get; set; }
            public int ParentCategoryId { get; set; }
            public string CategoryName { get; set; }
            public string AccessUrl { get; set; }
        }
        ProductCategory[] products = new ProductCategory[]
                {  
                    new ProductCategory { CategoryId = 2, ParentCategoryId = 1, CategoryName = "A", AccessUrl="http://www.google.com"},  
                    new ProductCategory { CategoryId = 3, ParentCategoryId = 1, CategoryName = "B", AccessUrl="http://www.facebook.com"}, 
                    new ProductCategory { CategoryId = 4, ParentCategoryId = 1, CategoryName = "C", AccessUrl="http://www.bing.com"}, 
                    new ProductCategory { CategoryId = 5, ParentCategoryId = 1, CategoryName = "D", AccessUrl="http://www.google.com"}, 
                    new ProductCategory { CategoryId = 6, ParentCategoryId = 1, CategoryName = "E", AccessUrl="http://www.google.com"}, 
                    new ProductCategory { CategoryId = 7, ParentCategoryId = 2, CategoryName = "A1", AccessUrl="http://www.facebook.com"}, 
                    new ProductCategory { CategoryId = 8, ParentCategoryId = 2, CategoryName = "A2", AccessUrl="http://www.yahoo.com"}, 
                    new ProductCategory { CategoryId = 9, ParentCategoryId = 2, CategoryName = "A3", AccessUrl="http://www.bing.com"}, 
                    new ProductCategory { CategoryId = 10, ParentCategoryId = 3, CategoryName = "B1",AccessUrl="http://www.facebook.com"}, 
                    new ProductCategory { CategoryId = 11, ParentCategoryId = 3, CategoryName = "B2",AccessUrl="http://www.yahoo.com"}, 
                    new ProductCategory { CategoryId = 12, ParentCategoryId = 3, CategoryName = "B3",AccessUrl="http://www.facebook.com"}, 
                    new ProductCategory { CategoryId = 13, ParentCategoryId = 4, CategoryName = "C1",AccessUrl="http://www.yahoo.com"}, 
                    new ProductCategory { CategoryId = 14, ParentCategoryId = 4, CategoryName = "C2",AccessUrl="http://www.bing.com"}, 
                    new ProductCategory { CategoryId = 15, ParentCategoryId = 4, CategoryName = "C3",AccessUrl="http://www.facebook.com"}, 
                    new ProductCategory { CategoryId = 16, ParentCategoryId = 5, CategoryName = "D1",AccessUrl="http://www.yahoo.com"}, 
                    new ProductCategory { CategoryId = 17, ParentCategoryId = 5, CategoryName = "D2",AccessUrl="http://www.bing.com"}, 
                    new ProductCategory { CategoryId = 18, ParentCategoryId = 5, CategoryName = "D3",AccessUrl="http://www.yahoo.com"}, 
                    new ProductCategory { CategoryId = 19, ParentCategoryId = 6, CategoryName = "E1",AccessUrl="http://www.facebook.com"}, 
                    new ProductCategory { CategoryId = 20, ParentCategoryId = 6, CategoryName = "E2",AccessUrl="http://www.google.com"},
                    new ProductCategory { CategoryId = 21, ParentCategoryId = 6, CategoryName = "E3",AccessUrl="http://www.bing.com"}
                };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindParentCategory();
            }
        }
        private void BindParentCategory()
        {
            var productCategories = from c in products
                                    where c.ParentCategoryId == 1
                                    select c;
            if (productCategories.Any())
            {
                rptCategory.DataSource = productCategories;
                rptCategory.DataBind();
            }
        }
        protected void rptParentItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var productCategory = e.Item.DataItem as ProductCategory;
            if (productCategory != null)
            {
                ViewState["ParentCategory"] = productCategory.CategoryName;
                var lnkCat = (HtmlGenericControl)e.Item.FindControl("lnkCat");
                if (lnkCat != null)
                {
                    lnkCat.Attributes.Add("onclick", "window.open('" + productCategory.AccessUrl + "','_self');");
                    //lnkCat.Attributes.Add("onclick", "window.open('" + productCategory.CategoryName + "','_blank');");
                }
                var childCategories = from c in products
                                      where c.ParentCategoryId == productCategory.CategoryId
                                      select c;

                if (childCategories.Any())
                {
                    var rptChildMenuItem = e.Item.FindControl("rptSubCategory") as Repeater;
                    if (rptChildMenuItem != null)
                    {
                        rptChildMenuItem.DataSource = childCategories;
                        rptChildMenuItem.DataBind();
                    }
                }
            }
        }
        protected void rptChildItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var productCategory = (ProductCategory)e.Item.DataItem;
                var hlkSubCategory = (HtmlAnchor)e.Item.FindControl("lnkChildCat");
                if (hlkSubCategory != null)
                {
                    string parrentCategory = ViewState["ParentCategory"].ToString();
                    hlkSubCategory.HRef = parrentCategory + "/" + productCategory.AccessUrl;
                }
            }
        }
    }
}